const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:"",
    phone:"",
    userid:""
  },
  //form表单事件
  bindSave:function(e){
    var name = e.detail.value.name;
    var phone = e.detail.value.phone;
    var userid = e.detail.value.userid;

    if (name == '') {
      wx.showToast({
        title: '请输入姓名',
        icon: 'none',
        duration: 2000,
      })
      return false
    } else if (phone == '') {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
        duration: 2000,
      })
      return false
    } else if (phone.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(phone)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    this.setData({
      name: name,
      phone: phone,
      userid:userid
    })
  },
  //点击提交按钮
  toqrcode: function () {
    // wx.navigateTo({
    //   url: '/pages/outcode/outcode',
    // })

    var that = this.data;
    app.globalData.phone = that.phone;
    app.globalData.name = that.name;
    // app.globalData.userid = that.userid;

    wx.cloud.callFunction({
      name: "outpeople",
      data: {
        name: that.name,
        phone: that.phone,
        userid: that.userid,
      },
      success(res) {
        wx.showModal({
          title: '提示',
          content: '注册成功，是否跳转登录页面',
          success: function (res) {
            if (res.confirm) {
              wx.navigateBack({})
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        console.log("成功", res.result)
      },
      fail(res) {
        wx.showToast({
          title: '注册失败，请检查信息是否有误！',
          icon: 'none'
        })
        console.log("失败", res)
      }
    })
  },
})